<?php

class Wyc_models_testmysql extends CI_Model
{
    public function testmysql($sql_date)
    {
        //echo "欢迎来到models——testmysql <br>";
        $sql = "SELECT * FROM food_order WHERE validay >= ? AND validay <= ?";
        $DB1 = $this->load->database('wyc', TRUE);
        $query = $DB1->query($sql, array($sql_date, $sql_date + 86400));
        //print_r($query->result());
        //echo json_encode($query->result());
        $new_arr = array();
        foreach ($query->result_array() as $row) {
            if ($new_arr[$row['department']] == null) {
                $new_arr[$row['department']] = array();

            }
            array_push($new_arr[$row['department']], $row['name']);
        }
        print_r($new_arr);
        // 关闭数据库
        $DB1->close();
    }


    public function testmysql_fengzu($sql_date)
    {
        //echo "欢迎来到models——testmysql_fengzu <br>";
        $sql = "SELECT * FROM food_order WHERE validay >= ? AND validay <= ?";
        $DB1 = $this->load->database('wyc', TRUE);
        $query = $DB1->query($sql, array($sql_date, $sql_date + 86400));
        $new_arr = array();
        foreach ($query->result_array() as $row) {
            if ($new_arr[$row['department']] == null) {
                $new_arr[$row['department']] = new phpClass();
            }

            // 计算早餐总人数
            if ($row['breakfast'] != 0) {
                $new_arr[$row['department']]->breakfast_sum++;
            }
            // 计算早餐未吃人数
            if ($row['breakfast'] == 1) {
                $new_arr[$row['department']]->breakfast_sum_no++;
                $new_arr[$row['department']]->arr_breakfast[] = $row['name'];
            }

            // 计算中餐总人数
            if ($row['lunch'] != 0) {
                $new_arr[$row['department']]->lunch_sum++;
            }
            // 计算中餐未吃人数
            if ($row['lunch'] == 1) {
                $new_arr[$row['department']]->lunch_sum_no++;
                $new_arr[$row['department']]->arr_lunch[] = $row['name'];
            }

            // 计算晚餐总人数
            if ($row['dinner'] != 0) {
                $new_arr[$row['department']]->dinner_sum++;
            }
            // 计算晚餐未吃人数
            if ($row['dinner'] == 1) {
                $new_arr[$row['department']]->dinner_sum_no++;
                $new_arr[$row['department']]->arr_dinner[] = $row['name'];
            }
        }
        //print_r($new_arr);
        $jsonstr = json_encode($new_arr, JSON_UNESCAPED_UNICODE);
        echo $jsonstr;
        // 关闭数据库
        $DB1->close();
    }

    public function testmysql_fengzu2($sql_date)
    {
        //echo "欢迎来到models——testmysql <br>";
        $sql = "SELECT * FROM food_order WHERE validay >= ? AND validay <= ?";
        // 连接‘wyc’数据库
        $DB1 = $this->load->database('wyc', TRUE);
        // 查询指定时间的时间戳内容
        $query = $DB1->query($sql, array($sql_date, $sql_date + 86400));


        // 创建一个数组来保存每一天的早中晚
        $new_arr = array();
        foreach ($query->result_array() as $row) {
            // 获取部门
            $bumen = $row['department'];


            // 一。计算早餐
            // 1.生成早餐数组
            $new_arr['早餐'];
            // 2.生成早餐的各个部门
            $new_arr['早餐'][$bumen];
            // 3.计算早餐总人数
            if ($row['breakfast'] != 0) {
                $new_arr['早餐'][$bumen]['报单人数']++;
            }
            // 4.计算早餐未吃人数
            if ($row['breakfast'] == 1) {
                $new_arr['早餐'][$bumen]['未吃人数']++;
                // 5.添加到未吃人员名单数组中
                $new_arr['早餐'][$bumen]['未吃人姓名'][]=$row['name'];
            }



            // 一。计算中餐
            $new_arr['中餐'];
            // 1.生成中餐数组
            $new_arr['中餐'];
            // 2.生成中餐的各个部门
            $new_arr['中餐'][$bumen];
            // 3.计算中餐总人数
            if ($row['lunch'] != 0) {
                $new_arr['中餐'][$bumen]['报单人数']++;
            }
            // 4.计算中餐未吃人数
            if ($row['lunch'] == 1) {
                $new_arr['中餐'][$bumen]['未吃人数']++;
                // 5.添加到未吃人员名单数组中
                $new_arr['中餐'][$bumen]['未吃人姓名'][]=$row['name'];
            }

            // 一。计算晚餐
            $new_arr['晚餐'];
            // 1.生成晚餐数组
            $new_arr['晚餐'];
            // 2.生成晚餐的各个部门
            $new_arr['晚餐'][$bumen];
            // 3.计算晚餐总人数
            if ($row['dinner'] != 0) {
                $new_arr['晚餐'][$bumen]['报单人数']++;
            }
            // 4.计算晚餐未吃人数
            if ($row['dinner'] == 1) {
                $new_arr['晚餐'][$bumen]['未吃人数']++;
                // 5.添加到未吃人员名单数组中
                $new_arr['晚餐'][$bumen]['未吃人姓名'][]=$row['name'];
            }

//            // 5.判断是否未吃人数是空，如果为空就默认给他们一个0
//            if ($new_arr['早餐'][$bumen]['未吃人数']==null){
//                $new_arr['早餐'][$bumen]['未吃人数']=0;
//            }
//
//            if ($new_arr['中餐'][$bumen]['未吃人数']==null){
//                $new_arr['中餐'][$bumen]['未吃人数']=0;
//            }
//
//            if ($new_arr['晚餐'][$bumen]['未吃人数']==null){
//                $new_arr['晚餐'][$bumen]['未吃人数']=0;
//            }
        }
        //print_r($new_arr);
        $jsonstr = json_encode($new_arr,JSON_UNESCAPED_UNICODE);
        echo $jsonstr;
        // 关闭数据库
        $DB1->close();
    }
}

class phpClass
{
    var $breakfast_sum = 0; // 早餐人数
    var $lunch_sum = 0; // 中餐人数
    var $dinner_sum = 0; // 晚餐人数

    var $breakfast_sum_no = 0; // 未吃早餐人数
    var $lunch_sum_no = 0; // 未吃中餐人数
    var $dinner_sum_no = 0; // 未吃晚餐人数

    var $arr_breakfast = array();// 没吃早餐姓名列表
    var $arr_lunch = array(); // 没吃中餐姓名列表
    var $arr_dinner = array(); // 没吃晚餐姓名列表
}